package use;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Login { //登录
    public void cusLogin() throws IOException {
        Scanner user = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = user.next();
        System.out.println("请输入密码:");
        String password = user.next();
        FileReader fr = new FileReader("information\\customer.txt");
        BufferedReader bfr = new BufferedReader(fr);
        String str;
        while (true) {
            str = bfr.readLine();
            if ((name + "," + password).equals(str)) {
                chooseUse();
                return;
            } else if (str == null) {
                int n=0;
                while (n==0) {
                    System.out.println("用户名或密码错误!");
                    System.out.println("请选择:" + "\n" + "1、重新登录2、退出系统");
                    String choose = user.next();
                    switch (choose) {
                        case "1":
                            System.out.println("请重新输入！");
                            cusLogin();
                            break;
                        case "2":
                            System.out.println("您已退出系统，欢迎下次使用！");

                            return;
                        default:
                            System.out.println("功能号不存在！请正确输入功能号！");
                            break;
                    }
                }
            }
        }
    }
    public void change() throws IOException {
        Scanner user = new Scanner(System.in);
        Personnel personnel = new Personnel();
        System.out.println("请选择:" + " " + "1、用户信息修改" + " " + "2、歌曲修改" + " " + "3、退出系统");
        String sa = user.next();
        switch (sa) {
            case "1":
                personnel.changePerson();
                break;
            case "2":
                personnel.choose();
                return;
            case "3":
                System.out.println("您已退出系统，欢迎下次使用！");
                return;
            default:
                System.out.println("功能号不存在！请正确输入功能号！");
                change();
                break;
        }
    }

    public void perLogin() throws IOException {
        Scanner user = new Scanner(System.in);
        System.out.println("请输入管理员姓名:");
        String name = user.next();
        System.out.println("请输入管理员密码:");
        String password = user.next();
        FileReader fr1 = new FileReader("information\\personnel.txt");
        BufferedReader bfr1 = new BufferedReader(fr1);
        String str;
        List<String> list = new ArrayList<>();
        int n = 0;
        while ((str = bfr1.readLine()) != null) {
            list.add(str);
        }
        FileReader fr2 = new FileReader("information\\personnel.txt");
        BufferedReader bfr2 = new BufferedReader(fr2);
        while ((str = bfr2.readLine()) != null) {
            String[] data = str.split(",");
            if (name.equals(data[0]) && password.equals(data[1])) {
                change();
                n = 1;
            }
        }
            if (n == 0) {
                System.out.println("用户名或密码错误!");
                while (true) {
                    System.out.println("请选择:" + "\n" + "1、重新登录 2、退出系统");
                    String choose = user.next();
                    switch (choose) {
                        case "1"://重新登录
                            System.out.println("请重新输入！");
                            System.out.println("请输入管理员姓名:");
                            String newName = user.next();
                            System.out.println("请输入管理员密码:");
                            String newPassword = user.next();
                            for (String sri : list) {
                                String[] sr = sri.split(",");
                                if (sr[0].equals(newName) && sr[1].equals(newPassword)) {
                                    change();
                                }
                            }
                            break;
                        case "2":
                            System.out.println("您已退出系统，欢迎下次使用！");
                            return;
                        default:
                            System.out.println("功能号不存在！请正确输入功能号！");
                            break;
                    }
                }
            }
        }
//    public void choose() throws IOException {
//        Change changer = new Change();
//        Scanner user = new Scanner(System.in);
//        String sd = user.next();
//        switch (sd) {
//            case "1":
//                changer.deleteCustomer();
//                break;
//            case "2":
//                changer.changePersonnel();
//                break;
//            default:
//                System.out.println("功能号不存在！请正确输入功能号！");
//                choose();
//                break;
//        }
//    }
    public void chooseUse()throws IOException{
        Change changer = new Change();
        Customer customer = new Customer();
        Scanner user = new Scanner(System.in);
        System.out.println("请选择您要进行的操作:" + "\n" + "1、点歌" + " " + "2、修改信息" + " " + "3、注销信息");
        String st = user.next();
        switch (st) {
            case "1":
                customer.chooseSong();
                return;
            case "2":
                changer.changeCustomer();
                break;
            case "3":
                changer.deleteCustomer2();
                return;
            default:
                System.out.println("功能号不存在！请正确输入功能号！");
                chooseUse();
                break;
        }
    }
}